#include <stdio.h>
#include <stdlib.h>

typedef short int bool;
#define true    1
#define false   0


#define MAX_DIS 101;

int P = 0;
long R = 0;
int mat[50][50];
int s[50];

void init()
{
    int i = 0, j = 0;

    for(i = 0; i < P; i++)
    {
        for(j = 0; j < P; j++)
        {
            mat[i][j] = MAX_DIS;
            mat[j][i] = MAX_DIS;
        }
    }
}

void showmat()
{
    int i = 0, j = 0;

    for(i = 0; i < P; i++)
    {
        for(j = 0; j < P; j++)
        {
            printf("%d\t", mat[i][j]);
        }
        printf("\n");
    }
}

bool getinput()
{
    long k = 0;
    int i = 0, j = 0, len = 0;

    scanf("%d %ld", &P, &R);
    if(P == 0)
    {
        return false;
    }
    //
    init();

    for(k = 0; k < R; k++)
    {
        scanf("%d %d %d", &i, &j, &len);
        if(mat[i-1][j-1] > len)
        {
            mat[i-1][j-1] = len;
            mat[j-1][i-1] = len;
        }
    }
    return true;
}

void getnextv(int* nexts, int* nexte)
{
    int i = 0, j = 0;
    int min = MAX_DIS;

    for(i = 0; i < P; i++)
    {
        if(s[i] == 0)
        {
            continue;
        }
        for(j = 0; j < P; j++)
        {
            if(s[j] == 1)
            {
                continue;
            }
            if(mat[i][j] < min)
            {
                min = mat[i][j];
                *nexts = i;
                *nexte = j;
            }
        }
    }
}

void process()
{
    int i = 0;
    int ncount = 0, nexts = -1, nexte = -1;
    int minlen = 0;

    //showmat();

    for(i = 0; i < P; i++)
    {
        s[i] = 0;
    }
    s[0] = 1;

    while(ncount < P-1)
    {
        getnextv(&nexts, &nexte);
        //printf("nexts:%d nexte:%d\n", nexts, nexte);
        if(nexts == -1 || nexte == -1)
        {
            break;
        }
        s[nexte] = 1;
        minlen += mat[nexts][nexte];
        ncount++;
    }

    printf("%d\n", minlen);
}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);

    while(getinput())
    {
        process();
    }
    return 0;
}
